#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 2010;

int f[N], a[N], b[N], n;

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
	n = in; rep(i, 1, n) a[i] = in, b[i] = in;
	memset(f, 0xcf, sizeof f); f[1] = 0;
	vec pot; rep(i, 1, n) if(b[i] > 0 && a[i] == 0) pot.pb(b[i]);
	sort(pot.begin(), pot.end(), greater < int >());
	int tot = pot.size() + 1;
	rep(i, 1, n) {
		if(a[i] == 0) continue; a[i]--;
		per(j, tot, 1) f[min(j + a[i], tot)] = max(f[min(j + a[i], tot)], f[j] + b[i]);
	}
	int ans = 0, res = 0;
	rep(i, 1, tot) {
		if(i <= pot.size()) res += pot[i - 1];
		ans = max(ans, f[i] + res);
	}
	cout << ans << endl; return 0;
}
